********************************************************************************
* CREATE ZQTAILS DATASET, USING Q DISTS, WITH ZC TAIL PROBABILITIES PER MONTH

/*
*Run this block if stand alone. If commented out, it is because this will be run from _ricardo_master.do
set more off, perm
clear
graph drop _all
local dir "/Users/rreis/Dropbox/05Shared_folders/HRR/HRR_InflDisaster/Ricardo"  // RR's directory
cd `dir'
local region = "EZ" 	// set region US or EZ
local updateIteration "Mar2022"
*/

local region `1'
local updateIteration `2'

*Redo monthly data and drop empty variables
u "$input/`region'_dist_Z_monthly", clear

gen day = day(date_stata)
gen month = month(date_stata)
gen year = year(date_stata)
sort year month day
    collapse (first) date_stata, by(year month)
    keep date_stata
    merge 1:m date using "$input/`region'_dist_Z_monthly.dta"
    keep if _merge == 3
    drop _merge
*drop I J K L M N O

// Generate 5-year cumulative tails
generate tails_5y = frequency if dist_identifier=="ZQ5" 

generate uptails_5y = tails_5y if support>=.04
egen tail4_5y = total(uptails_5y), by(date_stata)
replace uptails_5y=. if support<0.05
egen tail5_5y = total(uptails_5y), by(date_stata)
drop uptails_5y

generate dotails_5y = tails_5y if support<=0
egen tail0_5y = total(dotails_5y), by(date_stata)
replace dotails_5y=. if support>-0.01
egen tailm1_5y = total(dotails_5y), by(date_stata)
drop dotails_5y

generate dotails_5y = tails_5y if support<=0.01
egen tail1_5y = total(dotails_5y), by(date_stata)
drop dotails_5y

// Generate 5-year densities
g q1_5y = tails_5y if support==0.01
g q0_5y = tails_5y if support==0.0
g q4_5y = tails_5y if support==0.04
g q5_5y = tails_5y if support==0.05

drop tails_5y

// Generate 10-year cumulative tails
generate tails_10y = frequency if dist_identifier=="ZQ10" 

generate uptails_10y = tails_10y if support>=.04
egen tail4_10y = total(uptails_10y), by(date_stata)
replace uptails_10y=. if support<0.05
egen tail5_10y = total(uptails_10y), by(date_stata)
drop uptails_10y

generate dotails_10y = tails_10y if support<=0
egen tail0_10y = total(dotails_10y), by(date_stata)
replace dotails_10y=. if support>-0.01
egen tailm1_10y = total(dotails_10y), by(date_stata)
drop dotails_10y

generate dotails_10y = tails_10y if support<=0.01
egen tail1_10y = total(dotails_10y), by(date_stata)
drop dotails_10y

// Generate  10-year densities
g q1_10y = tails_10y if support==0.01
g q0_10y = tails_10y if support==0.0
g q4_10y = tails_10y if support==0.04
g q5_10y = tails_10y if support==0.05

drop tails_10y

// Collapse and drop all other variables
collapse date tail4_5y tail1_5y tail5_5y tailm1_5y tail0_5y tail4_10y tail5_10y tail1_10y tailm1_10y tail0_10y q4_5y q4_10y q5_5y q5_10y q1_5y q1_10y q0_5y q0_10y, by(date_stata)

// Generate year month variable
g date_ym = mofd(date_stata)
format date_ym %tm

//Create good labels
label data "Dataset of tail probabilities, at 5- and 10-year horizon, at -1,0,4,5"
label variable tail4_5y 	"Tail CDF: 1 - Q(pi^5=4)"
label variable tail1_5y 	"Tail CDF: Q(pi^5=1)"
label variable tail5_5y		"Tail CDF: 1 - Q(pi^5=5)"
label variable tailm1_5y	"Tail CDF: Q(pi^5=-1)"
label variable tail0_5y		"Tail CDF: Q(pi^5=0)"
label variable tail4_10y	"Tail CDF: 1 - Q(pi^10=4)"
label variable tail5_10y	"Tail CDF: 1 - Q(pi^10=5)"
label variable tail1_10y	"Tail CDF: Q(pi^10=1)"
label variable tailm1_10y	"Tail CDF: 1 - Q(pi^10=-1)"
label variable tail0_10y 	"Tail CDF: 1 - Q(pi^10=0)"
label variable q4_5y     	"PDF: q(pi^5=4)"
label variable q4_10y    	"PDF: q(pi^10=4)"
label variable q5_5y     	"PDF: q(pi^5=5)"
label variable q5_10y    	"PDF: q(pi^10=5)"
label variable q1_5y     	"PDF: q(pi^5=1)"
label variable q1_10y    	"PDF: q(pi^10=1)"
label variable q0_5y     	"PDF: q(pi^5=0)"
label variable q0_10y    	"PDF: q(pi^10=0)"
label variable date_ym		"Date in plot format"
label variable date_stata	"Date in Stata format"
label variable date	"Date in original format"
order date_ym , after(date_stata)

// save it
sleep 100
save "$input/`region'_ZQtails.dta", replace
